package TablePractice;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TableExam {
    public static void main(String args[]){
        Table A=new Table();
    }
}
class Table extends JFrame implements ActionListener{
    JTable  table;
    Object a[][];
    Object name[]={"姓名","语文","数学","英语","总成绩","平均成绩"};
    JButton button,button2;
    Table(){
        a = new Object[8][6];
        for (int i = 0;i<8;i++){
            for (int j=0;j<6;j++){
                if(j!=0){
                    a[i][j]="0";
                }else{
                    a[i][j]="姓名";
                }
            }
        }
        button=new JButton("计算总成绩");
        button2=new JButton("计算平均成绩");
        table=new JTable(a,name);
        button.addActionListener(this);
        button2.addActionListener(this);
        Container con = getContentPane();
        getContentPane().add(new JScrollPane(table),BorderLayout.CENTER);
        con.add(new JLabel("修改或录入数据后，需要回车确认"),BorderLayout.SOUTH);
        con.add(button,BorderLayout.SOUTH);
        con.add(button2,BorderLayout.NORTH);
        setSize(600,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==button){
            for (int i=0;i<8;i++){//这里是通过遍历二维数组，计算总成绩
                double sum=0;
                boolean b=true;
                for (int j=1;j<=3;j++){
                    try {
                        sum=sum+Double.parseDouble(a[i][j].toString());
                    }
                    catch (Exception ee){
                        b=false;
                        table.repaint();
                    }
                    if (b==true){
                        a[i][4]=""+sum;
                        table.repaint();
                    }
                }
            }
        }else if (e.getSource()==button2){
            for (int i=0;i<8;i++){//这里是通过遍历二维数组，计算平均成绩。
                double sum=0;
                boolean b=true;
                for (int j=1;j<=3;j++){
                    try {
                        sum=sum+Double.parseDouble(a[i][j].toString());
                    }
                    catch (Exception ee){
                        b=false;
                        table.repaint();
                    }
                    if (b==true){
                        a[i][5]=""+sum/j;
                        table.repaint();
                    }
                }
            }
            Object b[][];
            b=new Object[8][6];
            for (int i=0;i<8;i++){//这里通过冒泡排序让成绩由高到低排序
                for(int o=i+1;o<8;o++){
                    if (Double.parseDouble(a[o][4].toString()) > Double.parseDouble(a[i][4].toString())){
                        for(int z=0;z<=5;z++){
                            b[0][z]=a[o][z];
                            a[o][z]=a[i][z];
                            a[i][z]=b[0][z];
                        }
                    }
                }
            }
            table.repaint();
        }

    }
}